package cn.wsh.pet.service.impl;

import cn.wsh.basic.service.impl.BaseServiceImpl;
import cn.wsh.org.domain.Employee;
import cn.wsh.pet.domain.Pet;
import cn.wsh.pet.domain.PetDetail;
import cn.wsh.pet.domain.PetOnlineAuditLog;
import cn.wsh.pet.mapper.PetDetailMapper;
import cn.wsh.pet.mapper.PetMapper;
import cn.wsh.pet.mapper.PetOnlineAuditLogMapper;
import cn.wsh.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoyang
 * @since 2022-02-24
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void addPetAndDetail(Pet pet, Employee login){
        initPet(pet,login.getShop_id());
        //1.保存宠物信息
        petMapper.save(pet);
        //2.保存宠物详情信息
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPetId(pet.getId());
            petDetailMapper.save(detail);
        }
    }


    private void initPet(Pet pet,Long shopId) {
        pet.setShopId(shopId);
        //写封装逻辑
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }


    @Override
    @Transactional
    public void onsale(List<Long> ids) {
        //1.审核需要上架的宠物信息是否合法
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            StringBuilder note = new StringBuilder();
            /*if(!BaiduAuditUtils.TextCensor(pet.getName())){
                //3.不合法  一个宠物信息不合法，全部上架失败吗？ 不是，只是当前记录上架失败，其他合法的要成功
                note.append("宠物名字不合法！！");
            }*/
            //审核resources
            String resources = pet.getResources();
            /*if(!StringUtils.isEmpty(resources)){
                String[] split = resources.split(",");
                if(!BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888"+split[0])){
                    //3.不合法  一个宠物信息不合法，全部上架失败吗？ 不是，只是当前记录上架失败，其他合法的要成功
                    note.append("宠物图片不合法！");
                }
            }*/
            if(note.length() > 0){//说明审核失败
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setPetId(id);
                auditLog.setState(0);
                auditLog.setAuditTime(new Date());
                auditLog.setNote(note.toString());
                petOnlineAuditLogMapper.save(auditLog);
            }else{ //2.合法-调整状态、上架时间，保存审核日志
                Map<String, Object> map = new HashMap<>();
                map.put("id",id);//要对这些宠物下架
                map.put("onSaleTime", new Date());
                petMapper.onSale(map);
            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);//要对这些宠物下架
        map.put("offSaleTime", new Date());
        petMapper.offSale(map);
    }

}
